var msg = document.getElementById('msg');
var wsServer = 'ws://58.87.68.229:9502';
var websocket = new WebSocket(wsServer);
websocket.onopen = function (evt) {//onopen监听连接打开

}

websocket.onmessage = function (evt) {
    msg.innerHTML += evt.data + '<br>';//不断增添数据
    console.log('从服务器获取到的数据' + evt.data);
}

//监听连接关闭
websocket.onclose = function (evt) {
    console.log('服务器拒绝');
}

//监听连接错误信息
websocket.onerror = function (evt, e) {
    console.log('错误: ' + evt.data);
}

//发送信息
function send_msg () {
    var text = document.getElementById('text').value;//获取数据
    document.getElementById('text').value= '';//清空数据
    websocket.send(text);//向服务器发送数据
}

//发送昵称
function send_name () {
    var text = document.getElementById('myname').value;//获取数据
    websocket.send("#name#" + text);//向服务器发送数据
    var myTitle = document.getElementById('myTitle');
    myTitle.innerHTML = "IM " + text;
    alert("设置完成");
    var setName = document.getElementById('setName');
    setName.style.display = "none";
    var send_msg = document.getElementById('send_msg');
    send_msg.style.display = 'block';

}